package com.zpself.module.异步;

import com.zpself.module.异步.entity.Person;
import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author By ZengPeng
 * @Description 分析Future的用法
 * @date in  2021/3/15 20:34
 * @Modified By
 */
public class 分析Future的用法 {

    ExecutorService executorService = Executors.newFixedThreadPool(3);
    @Test
    public void  testRunableAdapter() throws Exception {
        //runable
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                System.out.println("努力可能也不一定有回报啊，但请加油！");
            }
        };
        FutureTask<Object> future2 = new FutureTask<>(runnable,new Person("张三",1,"男"));


        new Thread(future2).start();
        Object o = future2.get();
        System.out.println("执行结束---");
        System.out.println(o);
    }

    @Test
    public void  test() throws Exception {

        //runable
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                System.out.println("努力可能也不一定有回报啊，但请加油！");
            }
        };
        Callable<Object> callable = () -> "努力可能也不一定有回报啊，但请加油！";
        FutureTask<Object> future1 = new FutureTask<>(callable);
        FutureTask<Object> future2 = new FutureTask<>(runnable,new Person("张三",1,"男"));
        Future<Object> submit = executorService.submit(callable);
        Object o = submit.get();


        System.out.println(o);
        Thread.sleep(2000);
    }
}
